/**
 * ReportGear(2011)
 */
package com.reportgear.designer.editor.object;

import java.awt.BorderLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.List;

import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.event.DocumentListener;

import com.reportgear.designer.formula.pane.FormulaPane;
import com.reportgear.report.model.cell.value.FormulaObject;

/**
 * 公式编辑器
 * 
 * @version 1.0 2011-4-26
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since Report 1.0
 */
public class FormulaEditor extends Editor {
	private FormulaObject formulaObject;
	private JTextField currentTextField;

	public FormulaEditor() {
		this("");
	}

	public FormulaEditor(String name) {
		this.formulaObject = new FormulaObject("");
		this.setLayout(new BorderLayout());
		JPanel panel = new JPanel();
		panel.setLayout(new BorderLayout(2, 0));
		this.currentTextField = new JTextField(28);
		this.currentTextField.setText("=");
		panel.add(this.currentTextField, BorderLayout.CENTER);
		this.currentTextField.setEditable(false);
		this.currentTextField.addMouseListener(new MouseListener() {
			public void mouseClicked(MouseEvent evt) {
			}

			public void mouseEntered(MouseEvent evt) {
			}

			public void mouseExited(MouseEvent evt) {
			}

			public void mousePressed(MouseEvent evt) {
				FormulaPane localFormulaPane = new FormulaPane();
//				localFormulaPane.populate(FormulaEditor.this.columnNameClassList, FormulaEditor.this.formulaObject);
//				localFormulaPane.showWindow(SwingUtilities.getWindowAncestor(FormulaEditor.this),
//						new DialogActionListener(localFormulaPane) {
//							public void doOk() {
////								FormulaEditor.access$102(FormulaEditor.this, this.val$formulaPane.update());
////								FormulaEditor.this.setValue(FormulaEditor.this.formulaObject);
//							}
//						}).setVisible(true);
			}

			public void mouseReleased(MouseEvent evt) {
			}
		});
		add(panel, BorderLayout.CENTER);
		setValue(this.formulaObject);
		setName(name);
	}

	public Object getValue() {
		if ((this.formulaObject != null) && ("=".equals(this.formulaObject.getFormula()))) {
			return null;
		}

		return this.formulaObject;
	}

	public void setValue(Object value) {
		if (value == null) {
			this.formulaObject = new FormulaObject();
			this.currentTextField.setText("=");
		} else if (value instanceof FormulaObject) {
			this.formulaObject = ((FormulaObject) value);
			this.currentTextField.setText(value.toString());
		} else {
			if (!(value.toString().startsWith("="))) {
				return;
			}

			this.formulaObject = new FormulaObject(value.toString());
			this.currentTextField.setText(value.toString());
		}
	}

	public void addDocumentListener(DocumentListener paramDocumentListener) {
		this.currentTextField.getDocument().addDocumentListener(paramDocumentListener);
	}

	public boolean accept(Object formula) {
		return formula instanceof FormulaObject;
	}
}